/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.components;

import domain.controller.file.ImageController;
import domain.model.IMobCatObject;
import domain.model.file.Categoria;
import domain.model.file.Registro;
import java.util.Hashtable;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import util.J2meAppHelper;
import view.AppForm;

/**
 *
 * @author Victorino
 */
public class MObjectList extends List {

    private Hashtable elements = new Hashtable();
    private int BACK_INDEX;
    private AppForm returnVal;
    private Object returnParameter;
    private Command cmdBack = new Command("Voltar", Command.BACK, 0);

    public MObjectList() {
        super("", List.IMPLICIT);
    }

    public MObjectList(String title, int type) {
        super(title, type);
    }

    public MObjectList(String title) {
        super(title, List.IMPLICIT);
    }

    public void appendMObject(IMobCatObject obj) {
        appendMObject(obj, null);
    }

    public void setFirstSelected(){
        super.setSelectedIndex(0, true);
    }

    public void appendMObject(IMobCatObject obj, Image img) {

        String label = obj.getNome();
        if (obj instanceof Categoria) {
            if (img == null) {
                img = ImageController.getInstance().getCategoriaImagem();
            }
        } else if (obj instanceof Registro && img == null) {
            img = ImageController.getInstance().getRegistroImagem();
        } 
        int id = append(label, img);
        elements.put(new Integer(id), obj);
    }

    public void forceBack() {
        J2meAppHelper.getInstance().changeForm(returnVal, returnParameter);
    }

    public IMobCatObject getSelectedMObject() {
        if (getSelectedIndex() == BACK_INDEX) {
            forceBack();
            return null;
        }
        return (IMobCatObject) elements.get(new Integer(getSelectedIndex()));
    }

    public void appendBack(AppForm appFormReturn, Object parameter) {

        BACK_INDEX = append("Voltar", getBackImage());
        this.returnVal = appFormReturn;
        this.returnParameter = parameter;
        this.removeCommand(cmdBack);
        this.addCommand(cmdBack);
    }

    public void appendBack(AppForm appFormReturn) {
        appendBack(appFormReturn, null);
    }

    public void setSelectMObject(Integer mobjectId) {
    }

    private Image getBackImage() {
        return ImageController.getInstance().getVoltarImagem();

    }

    public boolean isBackCmdClick(Command cmd) {
        return cmd.getCommandType() == Command.BACK || BACK_INDEX == getSelectedIndex();
    }

    public void deleteAll() {
        super.deleteAll();
        elements.clear();
    }

    public void sortByLabelAlpha() {
        final int vectorSize = size();
        for (int i = 1; i < vectorSize; i++) {
            final IMobCatObject mObj = (IMobCatObject) elements.get(new Integer(i));
            int j = i - 1;
            for (; j >= 0; j--) {
                IMobCatObject r = (IMobCatObject) elements.get(new Integer(j));
                if (r.getNome().compareTo(mObj.getNome()) <= 0) {
                    break;
                }
                elements.put(new Integer(j + 1), r);
                set(j + 1, r.getNome(), getImage(j));
            }
            elements.put(new Integer(j + 1), mObj);
            set(j + 1, mObj.getNome(), getImage(i));
        }
    }
}
